<?php
class ModelSaleCouponRule extends Model {
	public function addCouponRule($data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_rule SET `name` = '" . $name . 
      		"', discount_type = '" . (int)$data['discount_type'] . 
      		"', discount_values = '" . $this->db->escape($data['discount_values']) . 
      		"', discount_points = '" . $this->db->escape($data['discount_points']) . 
      		"', max_use_time = '" . (int)$data['max_use_time'] . 
      		"', max_use_day = '" . (int)$data['max_use_day'] . 
      		"', category_ids = '" . $this->db->escape($data['category_ids']) . "'");
      	$coupon_rule_id = $this->db->getLastId();
		
      	foreach ($data['coupon_rule_description'] as $language_id => $value) {
      		$sql = "INSERT INTO " . DB_PREFIX . "coupon_rule_description SET coupon_rule_id = '" . (int)$coupon_rule_id . 
        		"', language_id = '" . (int)$language_id . 
        		"', display_name = '";
			if($value['display_name']){
				$sql .= $this->db->escape($value['display_name']);
			}else{
				$sql .= $name;
			}
			$sql .=  "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql);
      	}
	}
	
	public function editCouponRule($coupon_rule_id, $data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("UPDATE " . DB_PREFIX . "coupon_rule SET `name` = '" . $name . 
      		"', discount_type = '" . (int)$data['discount_type'] . 
      		"', discount_values = '" . $this->db->escape($data['discount_values']) . 
      		"', discount_points = '" . $this->db->escape($data['discount_points']) . 
      		"', max_use_time = '" . (int)$data['max_use_time'] . 
      		"', max_use_day = '" . (int)$data['max_use_day'] . 
      		"', category_ids = '" . $this->db->escape($data['category_ids']) . 
      		"' WHERE coupon_rule_id = '" . (int)$coupon_rule_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "coupon_rule_description WHERE coupon_rule_id = '" . (int)$coupon_rule_id . "'");
      	foreach ($data['coupon_rule_description'] as $language_id => $value) {
      		$sql = "INSERT INTO " . DB_PREFIX . "coupon_rule_description SET coupon_rule_id = '" . (int)$coupon_rule_id . 
        		"', language_id = '" . (int)$language_id . 
        		"', display_name = '";
			if($value['display_name']){
				$sql .= $this->db->escape($value['display_name']);
			}else{
				$sql .= $name;
			}
			$sql .=  "', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql);
      	}
	}
	
	public function getCouponRule($coupon_rule_id) {
      	$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_rule WHERE coupon_rule_id = '" . (int)$coupon_rule_id . "'");
		return $query->row;
	}
	
	public function getCouponRuleDescription($coupon_rule_id) {
		$coupon_rule_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_rule_description WHERE coupon_rule_id = '" . (int)$coupon_rule_id . "'");
		foreach ($query->rows as $result) {
			$coupon_rule_data[$result['language_id']] = array(
				'display_name' => $result['display_name'],
				'description' => $result['description']
			);
		}
		return $coupon_rule_data;
	}

	public function getCouponRules($data = array()) {
		$sql = "SELECT r.coupon_rule_id, r.`name`, r.discount_type, r.discount_values, " . 
			"r.discount_points, r.max_use_time, r.max_use_day, r.category_ids, rd.display_name FROM " . 
			DB_PREFIX . "coupon_rule r LEFT JOIN " . 
			DB_PREFIX . "coupon_rule_description rd ON r.coupon_rule_id = rd.coupon_rule_id AND rd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "'";
		
		$sql_params = '';
		if (isset($data['filter_display_name'])) {
			$sql_params .= " AND rd.display_name LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
		if($sql_params != ''){
			$sql_params = trim($sql_params);
			$sql_params = ltrim($sql_params, 'AND');
			$sql .= " WHERE " . $sql_params;
		}
		$sql .= " ORDER BY rd.display_name";
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}

	public function getTotalCouponRules() {
		$sql = "SELECT COUNT(*) AS total FROM " . 
			DB_PREFIX . "coupon_rule r LEFT JOIN " . 
			DB_PREFIX . "coupon_rule_description rd ON r.coupon_rule_id = rd.coupon_rule_id AND rd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "'";
		
		$sql_params = '';
		if (isset($data['filter_display_name'])) {
			$sql_params .= " AND rd.display_name LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
		if($sql_params != ''){
			$sql_params = trim($sql_params);
			$sql_params = ltrim($sql_params, 'AND');
			$sql .= " WHERE " . $sql_params;
		}

		$query = $this->db->query($sql);
		return $query->row['total'];
	}	
	
	public function autocompleteCouponRule($filter_display_name = '') {
		$sql = "SELECT r.coupon_rule_id, r.`name`, r.discount_type, r.discount_values, " . 
			"r.discount_points, r.max_use_time, r.max_use_day, r.category_ids, rd.display_name, rd.description FROM " . 
			DB_PREFIX . "coupon_rule r LEFT JOIN " . 
			DB_PREFIX . "coupon_rule_description rd ON r.coupon_rule_id = rd.coupon_rule_id AND rd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "'";

		if ($filter_display_name != '') {
			$sql .= " WHERE rd.display_name LIKE '" . $this->db->escape($filter_display_name) . "%'";
		}
			
		$sql .= " ORDER BY rd.display_name LIMIT 0, 20";
		
		$query = $this->db->query($sql);
		
		$coupon_data = array();
		foreach ($query->rows as $result) {
			$coupon_data[] = array(
				'coupon_rule_id'  => $result['coupon_rule_id'],
				'name'            => $result['name'],
				'display_name'    => $result['display_name'],
				'discount_type'   => $result['discount_type'],
				'discount_points' => $result['discount_points'],
				'discount_values' => $result['discount_values'],
				'max_use_time'    => $result['max_use_time'],
				'max_use_day'     => $result['max_use_day'],
				'category_ids'    => $result['category_ids'],
				'description'     => $result['description']
			);
		}
		
		return $coupon_data;
	}
	
}
?>